package p2pcdnsim.network.example;

import p2pcdnsim.exception.LinkAlreadyInsertedException;
import p2pcdnsim.exception.NodeAlreadyInsertedException;
import p2pcdnsim.exception.NodeUnexistentException;
import p2pcdnsim.network.ApplicationLayer;
import p2pcdnsim.network.GenericAddress;
import p2pcdnsim.network.LANBus;
import p2pcdnsim.network.Link;
import p2pcdnsim.network.NetworkLayer;
import p2pcdnsim.network.NetworkTopologyBuilder;
import p2pcdnsim.network.Node;
import p2pcdnsim.network.RouterNode;
import p2pcdnsim.network.ShortestPathRouterTableBuilder;
import p2pcdnsim.network.TransportLayer;
import desmoj.core.simulator.SimTime;

public class ExampleTopologyBuilder extends NetworkTopologyBuilder {

	@Override
	public void assignRouterTables() throws NodeUnexistentException {
		//Assign router tables
		ShortestPathRouterTableBuilder sp =  new ShortestPathRouterTableBuilder(topology);
		for(int i=1; i<=6; i++) {
			Node n = topology.getNode(i);
			n.getNetworkLayer().setRouterTable(sp.getRouterTable(i));
		}
	}

	@Override
	public void attributeNodeAddresses() throws NodeUnexistentException {
		//Attribute node addresses
		for(int i=1; i<=6; i++) {
			Node n = topology.getNode(i);
			n.getNetworkLayer().setNetworkAddress(new GenericAddress("AddressNode"+n.getNodeId()));
		}
	}

	@Override
	public void createLinks() throws NodeUnexistentException, LinkAlreadyInsertedException {
		Link a12 = topology.createLink(topology.getModel(),1,2,1000000,new SimTime(0.0001));
		topology.addLink(a12);// a12.activate(new SimTime(0.0));
		Link a21 = topology.createLink(topology.getModel(),2,1,1000000,new SimTime(0.0001));
		topology.addLink(a21);// a21.activate(new SimTime(0.0));
		
		Link a13 = topology.createLink(topology.getModel(),1,3,1000000,new SimTime(0.0001));
		topology.addLink(a13);// a13.activate(new SimTime(0.0));
		Link a31 = topology.createLink(topology.getModel(),3,1,1000000,new SimTime(0.0001));
		topology.addLink(a31);// a31.activate(new SimTime(0.0));
		Link a14 = topology.createLink(topology.getModel(),1,4,1000000,new SimTime(0.0001));
		topology.addLink(a14);// a14.activate(new SimTime(0.0));
		Link a41 = topology.createLink(topology.getModel(),4,1,1000000,new SimTime(0.0001));
		topology.addLink(a41);// a41.activate(new SimTime(0.0));

		Link a25 = topology.createLink(topology.getModel(),2,5,1000000,new SimTime(0.0001));
		topology.addLink(a25);// a25.activate(new SimTime(0.0));
		Link a52 = topology.createLink(topology.getModel(),5,2,1000000,new SimTime(0.0001));
		topology.addLink(a52);// a52.activate(new SimTime(0.0));
		Link a26 = topology.createLink(topology.getModel(),2,6,1000000,new SimTime(0.0001));
		topology.addLink(a26);// a26.activate(new SimTime(0.0));
		Link a62 = topology.createLink(topology.getModel(),6,2,1000000,new SimTime(0.0001));
		topology.addLink(a62);// a62.activate(new SimTime(0.0));
	

	}

	@Override
	public void createNodes() throws NodeAlreadyInsertedException {
		for(int i=1; i<=6; i++) {
			RouterNode n = new RouterNode(i);
			topology.addNode(n);
		}
	}

	@Override
	public void buildNetworkStack() throws NodeUnexistentException {
		for(int i=1; i<=6; i++) {
			Node n = topology.getNode(i);
			n.setNetworkLayer(new NetworkLayer(topology.getModel()));
			//n.getNetworkLayer().activate(new SimTime(0.0));
			n.getNetworkLayer().setTransportLayer(new TransportLayer(topology.getModel()));
			//n.getNetworkLayer().getTransportLayer().activate(new SimTime(0.0));
			n.getNetworkLayer().getTransportLayer().setApplicationLayer(new ApplicationLayer(topology.getModel()));
			//n.getNetworkLayer().getTransportLayer().getApplicationLayer().activate(new SimTime(0.0));;
		}
	}

	@Override
	public void createBus() throws NodeUnexistentException {
		for(int i=1; i<=6; i++) {
			RouterNode node = topology.getNode(i);
			LANBus bus = new LANBus(topology.getModel(), node , 1000000, new SimTime(0.0001));
			node.setLinkBus(bus);
		}
	}

}
